package com.example.customcomwidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * @author hdz
 * @date 2021/08/05 2:31 PM
 */
public class SelectTextSettingsItem extends LinearLayout implements View.OnTouchListener {

    private TextView tvContent;
    private int curIndex;
    public float touchX;
    private ListPopupWindowUtil listPopupWindowUtil;

    public SelectTextSettingsItem(Context context) {
        super(context);
        init(context, null);
    }

    public SelectTextSettingsItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SelectTextSettingsItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.select_text_settings_item, this);
        TextView tvTitle = this.findViewById(R.id.title);
        this.tvContent = this.findViewById(R.id.content);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelectTextSettingsItem);

        int titleColor = typedArray.getColor(R.styleable.SelectTextSettingsItem_titleColor, Color.BLACK);
        tvTitle.setTextColor(titleColor);

        int titleSize = typedArray.getDimensionPixelSize(R.styleable.SelectTextSettingsItem_titleSize, 12);
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);

        String titleText = typedArray.getString(R.styleable.SelectTextSettingsItem_titleText);
        tvTitle.setText(titleText);

        int titleGravity = typedArray.getInt(R.styleable.SelectTextSettingsItem_titleGravity, Gravity.CENTER_VERTICAL);
        tvTitle.setGravity(titleGravity);


        int contentColor = typedArray.getColor(R.styleable.SelectTextSettingsItem_contentColor, Color.BLACK);
        this.tvContent.setTextColor(contentColor);

        float contentSize = typedArray.getDimensionPixelSize(R.styleable.SelectTextSettingsItem_contentSize, 12);
        this.tvContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, contentSize);

        String contentText = typedArray.getString(R.styleable.SelectTextSettingsItem_contentText);
        this.tvContent.setText(contentText);

        int contentGravity = typedArray.getInt(R.styleable.SelectTextSettingsItem_contentGravity, Gravity.CENTER_VERTICAL | Gravity.END);
        this.tvContent.setGravity(contentGravity);

        int drawableId = typedArray.getResourceId(R.styleable.SelectTextSettingsItem_contentDrawable, 0);
        if (drawableId != 0) {
            Drawable contentDrawable = getResources().getDrawable(drawableId);
            if (contentDrawable != null) {
                int w = contentDrawable.getIntrinsicWidth();
                int h = contentDrawable.getIntrinsicHeight();

                int contentDrawableW = typedArray.getDimensionPixelSize(R.styleable.SelectTextSettingsItem_contentDrawableWidth, w);
                int contentDrawableH = typedArray.getDimensionPixelSize(R.styleable.SelectTextSettingsItem_contentDrawableWidth, h);

                contentDrawable.setBounds(0, 0, contentDrawableW, contentDrawableH);
                this.tvContent.setCompoundDrawables(null, null, contentDrawable, null);
            }
        }

        typedArray.recycle();

        setOnTouchListener(this);
    }

    public void setText(String str) {
        this.tvContent.setText(str);
    }

    public void setText(int resId) {
        this.tvContent.setText(resId);
    }

    public void setText(String str, int curIndex) {
        this.tvContent.setText(str);
        this.curIndex = curIndex;
    }

    public int getCurIndex() {
        return curIndex;
    }

    public void setListPopupWindowUtil(ListPopupWindowUtil listPopupWindowUtil) {
        this.listPopupWindowUtil = listPopupWindowUtil;
    }

    public ListPopupWindowUtil getListPopupWindowUtil() {
        return listPopupWindowUtil;
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            this.touchX = event.getRawX();
        }
        return false;
    }
}
